﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using VCommons.Encryptor;

namespace Standard
{
    /// <summary>
    /// 用户信息关键字
    /// </summary>
    public enum UserInfoKeys
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        UserID,
        /// <summary>
        /// 用户名
        /// </summary>
        Name,
        /// <summary>
        /// 用户邮件
        /// </summary>
        Email,
    }

    /// <summary>
    /// 用户实体
    /// 从cookies中返回实体对象，并提示写cookies的统一入口
    /// </summary>
    public class UserInfoImplement : ISimpleUserInfo
    {
        #region Consts and Fields

        /// <summary>
        /// cookies 分隔符号
        /// </summary>
        static char[] SPCHAR = { ',' };
        /// <summary>
        /// 国际化标准
        /// </summary>
        const int DEFAULTCULTURE = 86;
        #endregion

        #region Implement ISimpleUserInfo
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserID
        {
            get
            {
                return Utility.DecryptString(VCommons.Http.CookieHelper.Read(ClientHelper.SiteCookiesMainName, Enum.GetName(typeof(UserInfoKeys), UserInfoKeys.UserID)));
            }

        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string Name
        {
            get
            {
                return
                   System.Web.HttpUtility.UrlDecode(VCommons.Http.CookieHelper.Read(ClientHelper.SiteCookiesMainName, Enum.GetName(typeof(UserInfoKeys), UserInfoKeys.Name)));
            }

        }

        /// <summary>
        /// 用户 Email
        /// </summary>
        public string Email
        {
            get
            {
                return VCommons.Http.CookieHelper.Read(ClientHelper.SiteCookiesMainName, Enum.GetName(typeof(UserInfoKeys), UserInfoKeys.Email));
            }

        }


        /// <summary>
        /// 国际化ID
        /// </summary>
        public int CultureID
        {
            get
            {
                string cn = VCommons.Http.CookieHelper.Read(ClientHelper.SiteCookiesMainName, "CultureID");
                return string.IsNullOrEmpty(cn) ? DEFAULTCULTURE : int.Parse(cn);
            }

        }


        string jumpToUrl = null;

        /// <summary>
        /// 用户跳转地址
        /// </summary>
        public string returnURL
        {
            get
            {
                return this.jumpToUrl ?? VCommons.Http.CookieHelper.Read("returnURL");
            }
            set
            {
                this.jumpToUrl = value;
                VCommons.Http.CookieHelper.Write("returnURL", value);
            }
        }
        #endregion

        #region Methods
        #region 写入
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="uik">用户信息关键字</param>
        /// <param name="obj">写入值</param>
        static internal void Write(UserInfoKeys uik, object obj)
        {
            Write(uik, obj, true);
        }
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="uik">用户信息关键字</param>
        /// <param name="obj">写入值</param>
        /// <param name="isSave">是否永久化</param>
        static internal void Write(UserInfoKeys uik, object obj, bool isSave)
        {
            if (obj == null)
            {
                return;
            }
            if (isSave)
            {
                VCommons.Http.CookieHelper.WriteWithDomain(ClientHelper.SiteCookiesMainName,
                    Enum.GetName(typeof(UserInfoKeys), uik),
                    obj.ToString().Trim(),
                    525600,
                    ClientHelper.CookiesDomain
                );
            }
            else
            {
                VCommons.Http.CookieHelper.WriteWithDomain(ClientHelper.SiteCookiesMainName,
                    Enum.GetName(typeof(UserInfoKeys), uik),
                    obj.ToString().Trim(),
                    ClientHelper.CookiesDomain
                );
            }
        }
        #endregion
        #endregion
    }


}
